package com.core;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 管理提供给JS调用的接口类
 * 
 * @author 殷田
 *
 */
class InterfaceManager
{
	private Map< String, Object > interfaceMap = new HashMap< String, Object >();

	private Map< String, Map< String, Method > > interfaceMethodsMap = new HashMap< String, Map< String, Method > >();

	private final Object interfaceMapLock = new Object();
	private final Object interfaceMethodsMapLock = new Object();

	/**
	 * 添加接口对象
	 * 
	 * @param name
	 * @param api
	 * @return
	 */
	public boolean put( String name, Object api )
	{
		if ( name != null && name.length() >= 0 && api != null )
		{
			synchronized ( interfaceMapLock )
			{
				if ( !interfaceMap.containsKey( name ) )
				{
					interfaceMap.put( name, api );
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 获取接口对象
	 * 
	 * @param name
	 * @return
	 */
	public Object get( String name )
	{
		synchronized ( interfaceMapLock )
		{
			if ( interfaceMap.containsKey( name ) )
			{
				return interfaceMap.get( name );
			}
		}
		return null;
	}

	/**
	 * 移除接口对象
	 * 
	 * @param name
	 * @return
	 */
	public boolean remove( String name )
	{
		synchronized ( interfaceMapLock )
		{
			if ( !interfaceMap.containsKey( name ) )
			{
				return false;
			}
			interfaceMap.remove( name );
		}
		synchronized ( interfaceMethodsMapLock )
		{
			if ( interfaceMethodsMap.containsKey( name ) )
			{
				interfaceMethodsMap.remove( name );
			}
		}
		return true;
	}

	/**
	 * 清除
	 * 
	 * @return
	 */
	public void clear()
	{
		synchronized ( interfaceMapLock )
		{
			interfaceMap.clear();
		}
		synchronized ( interfaceMethodsMapLock )
		{
			interfaceMethodsMap.clear();
		}
	}

	public Method getMethod( String interfaceName, String methodName )
	{
		synchronized ( interfaceMethodsMapLock )
		{
			if ( !interfaceMethodsMap.containsKey( interfaceName )
							&& interfaceMap.containsKey( interfaceName ) )
			{
				interfaceMethodsMap.put( interfaceName, new HashMap< String, Method >() );
			}

			if ( interfaceMethodsMap.containsKey( interfaceName ) )
			{
				Map< String, Method > methods = interfaceMethodsMap.get( interfaceName );
				if ( methods.containsKey( methodName ) )
				{
					return methods.get( methodName );
				}
				else
				{
					Method method = getMethodByName( interfaceMap.get( interfaceName ),methodName );
					if ( method != null )
					{
						methods.put( methodName, method );
						return method;
					}
				}
			}
		}
		return null;
	}

	/**
	 * 根据方法名回去方法
	 * 
	 * @param feature
	 * @param methodName
	 * @return
	 */
	public Method getMethodByName( Object feature, String methodName )
	{
		Method targetMethod = null;
		try
		{
			Method[] methods = feature.getClass().getMethods();
			for ( Method method : methods )
			{
				if ( methodName.equals( method.getName() ) )
				{
					targetMethod = method;
					break;
				}
			}
		}
		catch ( Exception e )
		{
			e.printStackTrace();
		}
		return targetMethod;
	}
}
